home *** CD-ROM | disk | FTP | other *** search
- /*
- PixMapToPICT.c
- Saves a section of a PixMap (or BitMap) as a PICT file. You may substitute a
- BitMap handle for the PixMap handle. You are allowed to specify the PICT's pixel
- size and the color table used in translating the PixMap to a PICT. If you
- request zero pixelSize, the PICT's pixelSize will be the same as that of your
- PixMap. If the supplied color table handle is NULL then the Macintosh operating
- system default color table for that pixel size will be used. Note that PICT
- files include a color table. If you want to preserve the numerical value of
- each pixel then set pixelSize to zero (i.e. unchanged) and supply the color
- table of your PixMap, i.e. (**pm).pmTable.
-
- EXAMPLE:
- PixMapToPICT("image.pict",((CWindowPtr)window)->portPixMap,&window->portRect,0,NULL);
-
- BASED ON:
- Inside Macintosh: Imaging With QuickDraw, page 7-11
- Ortiz, G.A., and Johnson, C. (1994) A space-saving PICT trick. develop 20,63.
-
- HISTORY:
- 9/28/93 mike schechter wrote it, based in part on PixMapToPostScript.c
- 9/29/93 dgp polished it, substituting a GWorld for a CPort,
- so we can specify the pixel size and color table.
- 10/2/93 dgp added pixelSize and colorTable arguments.
- 9/5/94 dgp removed assumption in printf's that int==short.
- 6/30/95 dgp updated it to use OpenCPicture().
- */
- #define NEW_DEVICE 0
- #define DEBUG 1
-
- #include "VideoToolbox.h"
- //#include <Errors.h>
- PicHandle PixMapToPicture(PixMap **pm,Rect *rectPtr,int pixelSize,ColorTable **cTable);
-
- void PixMapToPICT(char *filename,PixMap **pm,Rect *rectPtr,int pixelSize,ColorTable **cTable)
- {
- long buffer[128],n;
- int i;
- FILE *file;
- PicHandle pic;
-
- pic=PixMapToPicture(pm,rectPtr,pixelSize,cTable);
-
- // save Picture as a PICT file
- file=fopen(filename,"wb");
- if(file==NULL)PrintfExit("%s %d: Error in opening file \"%s\".\n",__FILE__,__LINE__
- ,filename);
- // zero 512-byte header, because Apple says so.
- for(i=0;i<128;i++)buffer[i]=0;
- if(128!=fwrite(buffer,4,128,file))
- PrintfExit("%s %d: Error writing header of file \"%s\".\n",__FILE__,__LINE__,filename);
- n=GetHandleSize((Handle)pic);
- n++;n-=n%2; /* round up to multiple of 2 */
- HLock((Handle)pic);
- if(n!=fwrite(*pic,1,n,file))
- PrintfExit("%s %d: Error writing file \"%s\"\n",__FILE__,__LINE__,filename);
- fclose(file);
- SetFileInfo(filename,'PICT','ttxt');
-
- KillPicture(pic);
- }
-
- PicHandle PixMapToPicture(PixMap **pm,Rect *rectPtr,int pixelSize,ColorTable **cTable)
- {
- long value;
- int error;
- PicHandle pic;
- CGrafPtr oldPort;
- GDHandle oldDevice;
- GWorldPtr world;
- GWorldFlags flags;
- OpenCPicParams params;
-
- Gestalt(gestaltQuickdrawVersion,&value);
- if(value<gestalt32BitQD)
- PrintfExit("%s %d: Require 32-bit QuickDraw.\n",__FILE__,__LINE__);
- if(pixelSize==0)if((**pm).rowBytes & 0x8000) // Pixmap or Bitmap?
- pixelSize=(**pm).pixelSize; // Pixmap
- else pixelSize=1; // Bitmap
- GetGWorld(&oldPort,&oldDevice);
- flags=0;
- error=NewGWorld(&world,pixelSize,rectPtr,cTable,NULL,flags|useTempMem);
- if(error)error=NewGWorld(&world,pixelSize,rectPtr,cTable,NULL,flags);
- if(error==cTempMemErr || error==cNoMemErr || error==memFullErr)
- PrintfExit("%s %d: not enough memory; reduce rect or pixelSize.\n",__FILE__,__LINE__);
- if(error)PrintfExit("%s %d: NewGWorld error %d.\n",__FILE__,__LINE__,error);
- LockPixels(GetGWorldPixMap(world));
- SetGWorld(world,NULL);
- params.srcRect=*rectPtr; // best rectangle for displaying this picture
- params.hRes= 0x00480000; // horizontal resolution
- params.vRes= 0x00480000; // vertical resolution
- params.version=-2; // always set this field to -2
- params.reserved1=0; // this field is unused
- params.reserved2=0; // this field is unused
- CopyBitsQuickly((BitMap *)*pm,(BitMap *)*GetGWorldPixMap(world),rectPtr,rectPtr,srcCopy,NULL);
- pic=OpenCPicture(¶ms); // start creating the picture
- ClipRect(rectPtr);
- CopyBits((BitMap *)*GetGWorldPixMap(world),(BitMap *)*GetGWorldPixMap(world),rectPtr,rectPtr,srcCopy,NULL);
- ClosePicture();
- SetGWorld(oldPort,oldDevice);
- DisposeGWorld(world);
- error=QDError();
- if(error)PrintfExit("%s %d: QDError %d.\n",__FILE__,__LINE__,(int)QDError());
- if(EmptyRect(&(*pic)->picFrame) && !EmptyRect(rectPtr))
- PrintfExit("%s %d: out of memory. Reduce rect or pixelSize.\n",__FILE__,__LINE__);
- return pic;
- }
-